package org.yray.sip.base;



import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {
    private RegexUtils(){}

    public  static final String REGEX_EXT2=":[0-9]{1,5};";
    public  static final String REGEX_EXT="sip:[0-9]{5}";
    public  static final String REGEX_IP_PORT="((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}:[0-9]{2,5}";
    public  static final String REGEX_IP_AT="@((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
    public  static final String REGEX_IP="((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
    public static String findIpPort(String line) {
        if(line==null)
            return null;
        String pattern = REGEX_IP_PORT;
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);
        if(m.find())
        {
            return m.group();
        }
        return null;
    }



    public static String findOne(String line,String reg) {
        if(line==null)
            return null;
        Pattern r = Pattern.compile(reg);
        Matcher m = r.matcher(line);
        if(m.find())
        {
            return m.group();
        }
        return null;
    }

    public static String findPortByIpPort(String line) {
       String ipPort=findIpPort(line);
       if(StringUtils.isEmpty(ipPort))
           return null;
       return ipPort.split(MsgEle.ELE_SEPARATOR)[1];
    }

    public static String findExt(String line) {
        if(line==null)
            return null;
        String pattern = REGEX_EXT;
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);
        if(m.find())
        {
            return m.group().split(":")[1];
        }
        return null;
    }

    public static String findIpByIpPort(String line) {
        String ipPort=findIpPort(line);
        if(StringUtils.isEmpty(ipPort))
            return null;
        return ipPort.split(MsgEle.ELE_SEPARATOR)[0];
    }
}
